﻿#region Usings
using System;
using System.Linq;
using System.Threading;

using BK.Model;
using BK.Repositories.Abstract;
using BK.Repositories.Factory;
#endregion

namespace BK.SandBox
{
    public class CategoriesCreator
    {
        #region Fields
        private bool _pendingToStop;
        #endregion

        #region Instance Methods
        public void Create()
        {
            while (!_pendingToStop)
            {
                Console.WriteLine(string.Format("{0}", GetType()));
                IUsersRepository usersRepository = RepositoryFactory.Get<IUsersRepository>();
                User user = usersRepository.GetAll().ToArray().First();

                Category category = new Category
                    {
                        Name = Guid.NewGuid().ToString(),
                        User = user
                    };

                ICategoriesRepository categoriesRepository = RepositoryFactory.Get<ICategoriesRepository>();
                categoriesRepository.Save(category);
            }
        }

        public void Start()
        {
            Thread thread1 = new Thread(Create);
            thread1.Start();

            Thread thread2 = new Thread(Create);
            thread2.Start();
        }

        public void Stop()
        {
            _pendingToStop = true;
        }
        #endregion
    }

    public class TargetsCreator
    {
        #region Fields
        private bool _pendingToStop;
        #endregion

        #region Instance Methods
        public void Create()
        {
            while (!_pendingToStop)
            {
                Console.WriteLine(string.Format("{0}", GetType()));
                IUsersRepository usersRepository = RepositoryFactory.Get<IUsersRepository>();
                User user = usersRepository.GetAll().ToArray().First();

                Target target = new Target
                    {
                        Name = Guid.NewGuid().ToString(),
                        User = user
                    };

                ITargetsRepository targetsRepository = RepositoryFactory.Get<ITargetsRepository>();
                targetsRepository.Save(target);

                Category category = new Category
                    {
                        Name = Guid.NewGuid().ToString(),
                        User = user
                    };

                ICategoriesRepository categoriesRepository = RepositoryFactory.Get<ICategoriesRepository>();
                categoriesRepository.Save(category);
            }
        }

        public void Start()
        {
            Thread thread = new Thread(Create);
            thread.Start();
        }

        public void Stop()
        {
            _pendingToStop = true;
        }
        #endregion
    }

    public class B
    {
        #region Fields
        private Timer _timer;
        #endregion

        #region Instance Methods
        public void Start()
        {
            _timer = new Timer(OnTimer, null, 2000, 2000);
        }

        public void Stop()
        {
            _timer.Dispose();
        }

        private void OnTimer(object sender)
        {
            while (true)
            {
                Console.WriteLine(string.Format("{0}", GetType()));
                Thread.Sleep(new Random().Next(2000));
                IUsersRepository usersRepository = RepositoryFactory.Get<IUsersRepository>();
                User user = usersRepository.GetAll().ToArray().First();

                Target target = new Target
                    {
                        Name = Guid.NewGuid().ToString(),
                        User = user
                    };

                ITargetsRepository targetsRepository = RepositoryFactory.Get<ITargetsRepository>();
                targetsRepository.Save(target);

                Category category = new Category
                    {
                        Name = Guid.NewGuid().ToString(),
                        User = user
                    };

                ICategoriesRepository categoriesRepository = RepositoryFactory.Get<ICategoriesRepository>();
                categoriesRepository.Save(category);
            }
        }
        #endregion
    }

    public class A
    {
        #region Fields
        private Timer _timer;
        #endregion

        #region Instance Methods
        public void Start()
        {
            _timer = new Timer(OnTimer, null, 2000, 2000);
        }

        public void Stop()
        {
            _timer.Dispose();
        }

        private void OnTimer(object sender)
        {
            while (true)
            {
                Thread.Sleep(new Random().Next(2000));
                Console.WriteLine(string.Format("{0}", GetType()));
                IUsersRepository usersRepository = RepositoryFactory.Get<IUsersRepository>();
                User user = usersRepository.GetAll().ToArray().First();

                Target target = new Target
                    {
                        Name = Guid.NewGuid().ToString(),
                        User = user
                    };

                ITargetsRepository targetsRepository = RepositoryFactory.Get<ITargetsRepository>();
                targetsRepository.Save(target);

                Category category = new Category
                    {
                        Name = Guid.NewGuid().ToString(),
                        User = user
                    };

                ICategoriesRepository categoriesRepository = RepositoryFactory.Get<ICategoriesRepository>();
                categoriesRepository.Save(category);
            }
        }
        #endregion
    }

    internal class Program
    {
        #region Class Methods
        private static void Main(string[] args)
        {
            //            CategoriesCreator categoriesCreator = new CategoriesCreator();
            //            categoriesCreator.Start();
            //
            //            TargetsCreator targetsCreator = new TargetsCreator();
            //            targetsCreator.Start();

            A a = new A();
            B b = new B();

            a.Start();
            b.Start();

            Console.WriteLine("Press enter to stop...");
            Console.ReadLine();

            a.Stop();
            b.Stop();

            //            categoriesCreator.Stop();
            //            targetsCreator.Stop();
        }
        #endregion
    }
}